import { createRouter, createWebHistory } from 'vue-router'
import ViewUIPlus from 'view-ui-plus'
import { useTabStore } from '@/stores/tabs'
import { useRouteStore } from '@/stores/route.js'
import _ from 'lodash'
import http from '@/util/http'

const router = createRouter({
    history: createWebHistory(
        import.meta.env.BASE_URL),
    routes: [{
            path: '/',
            name: 'Login',
            component: () =>
                import ('../views/Basic/system/Login.vue'),
        },
        {
            path: '/Main',
            name: 'Main',
            component: () =>
                import ('../views/Basic/system/Main.vue'),
            children: [{
                    path: '/Basic/System/Index',
                    name: 'SystemIndex',
                    component: () =>
                        import ('../views/Basic/system/Index.vue'),
                    meta: { title: '首页' },
                },
                {
                    path: '/Settlement/List/Detail/:bah',
                    name: 'ListDetail',
                    component: () =>
                        import ('../views/Settlement/list/Detail.vue'),
                    meta: { title: '清单详情' },
                },
                {
                    path: '/Settlement/newList/Detail',
                    name: 'newListDetail',
                    component: () =>
                        import ('../views/Settlement/newList/Detail.vue'),
                    meta: { title: '清单详情' },
                },
                {
                    path: '/QualityControl/list/configurationQc',
                    name: 'configurationQc',
                    component: () =>
                        import ('../views/QualityControl/list/configurationQc.vue'),
                    meta: { title: '质控详情' },
                },
                {
                    path: '/Statistic/item/index',
                    name: 'StatisticDetail',
                    component: () =>
                        import ('../views/Statistic/item/index.vue'),
                    meta: { title: '分析详情' },
                },
            ],
        },
    ],
})
const modules =
    import.meta.glob('../views/*/*/*.vue')

router.beforeEach((to, from, next) => {
    const store = useRouteStore()
    ViewUIPlus.LoadingBar.start()
    if (to.path == '/') {
        next()
    } else {
        var token = sessionStorage.getItem('token')
        if (token === '' || token === null) {
            next('/')
        } else {
            if (store.route.data.length === 0) {
                http.get('/Basic/Menu/RouteList', {}, (response) => {
                    store.AddRoute(response.data.data)
                })
            }
            store.route.data.forEach((item) => {
                router.addRoute('Main', {
                    path: item.path,
                    name: item.name,
                    component: modules[`../views${item.component}.vue`],
                    meta: { title: item.title, keepAlive: true },
                })
            })
            next()
        }
    }
})

router.afterEach((to, from) => {
    const store = useTabStore()
    if (from.name === undefined) {
        router.push('/Basic/System/Index')
        ViewUIPlus.LoadingBar.finish()
        return
    }
    if (to.meta.title != '' && to.name != from.name) {
        store.AddTab({
            title: to.meta.title,
            key: to.path,
            name: to.name,
        })

        if (_.some(store.tabs, { title: to.meta.title })) {
            store.SetTabKey(to.path)
        }
    }
    ViewUIPlus.LoadingBar.finish()
})

export default router